package com.simpleplayer;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.transition.Slide;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.simpleplayer.MediaPlayer.MediaParam;
import com.simpleplayer.MediaPlayer.SimplePlayer;
import com.simpleplayer.engine.PlayerEngine;
import com.simpleplayer.services.LogUtil;

public class VideoActivity extends Activity {
    private static final String TAG = "VideoActivity";

    private SimplePlayer mPlayer;
    private ViewGroup mContainer;
    private boolean mPauseForExit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        LogUtil.d(TAG, "Activity " + this + " create");
        super.onCreate(savedInstanceState);

        initWindow();
        setContentView(R.layout.empty);
        mContainer = (ViewGroup)findViewById(R.id.main_container);
        handleIntent();
    }

    @Override
    protected void onStart() {
        LogUtil.d(TAG, "Activity " + this + " start");
        super.onStart();
    }

    @Override
    protected void onResume() {
        LogUtil.d(TAG, "Activity " + this + " resume");
        super.onResume();

        if (mPauseForExit) {
            mPlayer.toggle();
        }
    }

    @Override
    protected void onPause() {
        LogUtil.d(TAG, "Activity " + this + " pause");
        super.onPause();

        if (mPlayer != null && mPlayer.isPlaying() &&
                mPlayer.getCurrentDisPlayMode() != SimplePlayer.FLOATWINDOW) {
            mPlayer.toggle();
            mPauseForExit = true;
        }
    }

    @Override
    protected void onStop() {
        LogUtil.d(TAG, "Activity " + this + " stop");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        LogUtil.d(TAG, "Activity " + this + " destroy");
        super.onDestroy();
        if (mPlayer != null) {
            mPlayer.stop();
            mPlayer = null;
        }
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 横屏，自动进入fullscreen模式
            LogUtil.d(TAG, "检测到屏幕方向切换为横屏");
            if (mPlayer != null && mPlayer.getCurrentDisPlayMode() != SimplePlayer.FLOATWINDOW) {
                mPlayer.switchDisplayModeTo(SimplePlayer.FULLSCREEN);
            }
        } else {
            // 竖屏，不更新
            LogUtil.d(TAG, "检测到屏幕方向为竖屏");
        }

        if ((newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES) {
            // 进入深色模式，不更新
            LogUtil.d(TAG, "检测到系统主题为深色模式");
        }
    }

    private void initWindow() {
        // 启用窗口内容过度
        Slide enterSlide = new Slide(Gravity.END);
        Slide exitSlide = new Slide(Gravity.START);
        enterSlide.setDuration(400);
        exitSlide.setDuration(400);
        getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
        getWindow().setEnterTransition(enterSlide);
        getWindow().setReturnTransition(exitSlide);

        // 隐藏底部导航栏
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
        getWindow().getDecorView().setSystemUiVisibility(uiOptions);
    }

    private void handleIntent() {
        // Get intent, action and MIME type
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_VIEW.equals(action) && type != null) {
            if (type.startsWith("video")) {
                String videoUri = intent.getData().toString();
                createPlayer(videoUri, mContainer, true);
            }
        } else {
            String uri = intent.getStringExtra("video-src");
            mPlayer = createPlayer(uri, mContainer, false);
        }
    }

    private SimplePlayer createPlayer(String uri, ViewGroup container, boolean forceFullscreen) {
        MediaParam param;
        if (forceFullscreen) {
            // 强制全屏，例如从外部应用打开
            param = new MediaParam(uri, true,SimplePlayer.FULLSCREEN);
        } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // 手机横屏
            param = new MediaParam(uri, true, SimplePlayer.FULLSCREEN);
        } else {
            param = new MediaParam(uri, true);
        }
        mPlayer = new SimplePlayer(this, param, container);
        return mPlayer;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (getReferrer().toString().equals("android-app://com.simpleplayer")) {
                // 非三方app启动，返回主页
                Intent intent = new Intent(this, HomeActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(intent);
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
